﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 MDT.LifeSewagePlatform.Service
 * 接口名称 AlaramRuleService
 * 开发人员：-nhy
 * 创建时间：2022/3/31 11:40:13
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace MDT.LifeSewagePlatform.Service
{
    public class AlaramRuleService:BaseService,IAlaramRuleService
    {
        [Autowired]
        protected IAlaramRuleRepository ruleRepository { get; set; }

        [Autowired]
        protected IMapper mapper { get; set; }

        public AlaramRuleService()
        {
             
        }
        /// <summary>
        /// 创建规则
        /// </summary>
        /// <param name="alaramRuleDto"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<BaseMessage> AddAsync(AlaramRuleCreateDto alaramRuleDto,int userId)
        {
            var entity = mapper.Map<AlaramRuleEntity>(alaramRuleDto);
            await ruleRepository.Create(entity,userId);
            if (await ruleRepository.SaveChangesAsync())
            {
                return Ok200("规则创建成功!");
            }
            return BadRequest400("创建失败!");
        }

        public async Task<BaseMessage> UpdateAsync(int Id,AlaramRuleCreateDto alaramRuleDto, int userId)
        {
            if (await ruleRepository.AnyAsync(rule=>rule.ID.Equals(Id))==false)
            {
                return BadRequest400("更新失败!");
            }
            var entity =await ruleRepository.FirstOrDefaultAsync(rule=>rule.ID.Equals(Id));
            mapper.Map(alaramRuleDto,entity);
            await ruleRepository.Update(entity, userId);
            if (await ruleRepository.SaveChangesAsync())
            {
                return Ok200("更新成功!");
            }
            return BadRequest400("更新失败!");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<BaseMessage> DeleteAsync(int Id)
        {
            if (await ruleRepository.AnyAsync(rule => rule.ID.Equals(Id)) == false)
            {
                return BadRequest400("删除!");
            }
            await ruleRepository.Delete(rule => rule.ID.Equals(Id));
            if (await ruleRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }
        /// <summary>
        /// 软删除成功
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<BaseMessage> SoftDeleteAsync(int Id,int UserId)
        {
            if (await ruleRepository.AnyAsync(rule => rule.ID.Equals(Id)) == false)
            {
                return BadRequest400("删除!");
            }
            await ruleRepository.SoftDelete(rule => rule.ID.Equals(Id),UserId);
            if (await ruleRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }

        public async Task<BaseMessage> GetRulesAsync(AlaramRuleParameter Params)
        {
            var query=ruleRepository.GetAlaramRules(t=>true);

            if (string.IsNullOrEmpty(Params.DisplayName)==false)
            {
                query=query.Where(rule=>rule.DisplayName.Contains(Params.DisplayName));
            }
            return Ok200("查询成功!",await PageList<AlaramRuleView>.CreateAsync(query,Params));
        }
    }
}
